package tv.acgn.advertisement.publish.dao.dsl;

import java.time.LocalDateTime;

import com.querydsl.jpa.impl.JPAQueryFactory;
import org.springframework.beans.factory.annotation.Autowired;
import org.springframework.stereotype.Service;
import tv.acgn.advertisement.publish.po.AdUnitPO;
import tv.acgn.advertisement.publish.po.QAdUnitPO;
import tv.acgn.advertisement.publish.vo.adunit.AdUnitRequestVO;

/**
 * @author : junG
 * @program : advertisement-ad-pub-dao-dsl
 * @description : AdUnit 用于复杂条件和参数持久化的操作类
 * @date : 2019-05-26 01:10
 **/
@Service
public class AdUnitDslDao {

    @Autowired
    private JPAQueryFactory queryFactory;

    /**
     * <h2>根据条件参数更新推广单元 AdUnit</h2>
     *
     * @param adUnitPO  [1]
     * @param requestVO [2]
     *
     * @return {@link Long}
     *
     * @author junG
     * @description 根据条件参数更新推广单元 AdUnitPO 为空的字段则不更新
     * @date 0:14 2019/6/2
     */
    public long update(AdUnitPO adUnitPO, AdUnitRequestVO requestVO) {
        QAdUnitPO qAdUnitPO = QAdUnitPO.adUnitPO;
        long result = queryFactory.update(qAdUnitPO)
            .set(qAdUnitPO.unitName, requestVO.getUnitName())
            .set(qAdUnitPO.positionType, requestVO.getPositionType())
            .set(qAdUnitPO.unitStatus, requestVO.getUnitStatus())
            .set(qAdUnitPO.budget, requestVO.getBudget())
            .set(qAdUnitPO.updateTime, LocalDateTime.now()).execute();
        return result;
    }

}
